package com.cloud.basic.Schedule;

import cn.hutool.core.date.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import java.util.Date;

/**
 * @program: basic
 * @description: 定时任务工具类
 * @author: Cloud
 * @create: 2021/5/20 11:46:04
 */
@Slf4j
public class QuartzUtil {

    public static void createTask(Date startDate,Date endDate,String flag){
        // 定义一个jobDetail（这里表示定义一个任务）
        JobDetail jobDetail = JobBuilder.newJob(QuartzTasks.class).withIdentity("hello","group").usingJobData("name","roswell").usingJobData("flag", flag).build();
        // 时间间隔
        Long duration = (endDate.getTime()-startDate.getTime())/1000;
        System.out.println(startDate);
        System.out.println(endDate);
        System.out.println("定时任务即将开启！");
        // 定义一个触发器  注：实际执行了两次withRepeatCount(1)
        DailyTimeIntervalTrigger trigger = TriggerBuilder.newTrigger().withIdentity("trigger", "group").startNow()
                .withSchedule(DailyTimeIntervalScheduleBuilder.dailyTimeIntervalSchedule()
                        .startingDailyAt(TimeOfDay.hourAndMinuteAndSecondFromDate(startDate))
                        .endingDailyAt(TimeOfDay.hourAndMinuteAndSecondFromDate(endDate))
                        .withRepeatCount(2).withIntervalInSeconds(duration.intValue()).onEveryDay()).build();
        try {
            // 定时任务列表
            Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
            scheduler.scheduleJob(jobDetail,trigger);
            // 运行一段时间关闭
            scheduler.start();
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }

    public static void createResultTask(String deviceName,String cavity) {
        // 当前时间，及促发s6f11的power结束时间点
        Date date = new Date();
        String dateString = DateUtil.format(date, "yyyy-MM-dd HH:mm:ss.SSS");
        System.out.println("任务创建时间 = " + dateString);
        // 创建一个延迟保存结果的任务
        // 定义一个jobDetail（这里表示定义一个任务）
        JobDetail jobDetail = JobBuilder.newJob(QuartzTasks1.class)
                .withIdentity(cavity+"-result-job", cavity+"-result-group")
                .usingJobData("deviceName", deviceName)
                .usingJobData("cavity",cavity)
                .usingJobData("powerStopTime",dateString)
                .build();
        // 延迟30s
        date = DateUtil.offsetSecond(date,20);
        log.info("延迟结果开始时间：{}", DateUtil.format(date,"yyyy-MM-dd HH:mm:ss.SSS"));

        // 定义一个触发器  注：实际执行了两次withRepeatCount(1)
        SimpleTrigger trigger = (SimpleTrigger) TriggerBuilder.newTrigger()
                .withIdentity(cavity+"-result-trigger", cavity+"-result-group")
                .startAt(date)
                .build();
        try {
            // 定时任务列表
            Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
            scheduler.scheduleJob(jobDetail, trigger);
            // 运行一段时间关闭
            scheduler.start();
            log.info("定时任务调度时间：{}",new Date());
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }
}
